package com.bree.erp.entity;

import java.util.Set;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

@Entity
@IdClass(PermissionId.class)
@NamedQuery(name = Permission.QUERY_FIND_USERPROJECTS, 
query = "SELECT p.project FROM Permission p join p.groups g WHERE p.user = :user and g = :group")
public class Permission implements CrudId<PermissionId> {
	
	private static final long serialVersionUID = 1L;
	
	public static final String QUERY_FIND_USERPROJECTS = "findUserAssignedProjects";

	@Transient
	private PermissionId id;
	
	@Id
	@OneToOne
	private User user;
	
	@Id
	@OneToOne
	private Project project;
	
	@ElementCollection
	@Enumerated(EnumType.STRING)
	private Set<GroupType> groups;
	
	public Permission() {
		
	}
	
	public Permission(User user, Project project) {
		this.user = user;
		this.project = project;
	}

	@Override
	public PermissionId getId() {
		
		if(id == null) {
			id = new PermissionId(user.getId(), project.getId());
		}
		
		
		return id;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the project
	 */
	public Project getProject() {
		return project;
	}

	/**
	 * @param project the project to set
	 */
	public void setProject(Project project) {
		this.project = project;
	}

	/**
	 * @return the groups
	 */
	public Set<GroupType> getGroups() {
		return groups;
	}

	/**
	 * @param groups the groups to set
	 */
	public void setGroups(Set<GroupType> groups) {
		this.groups = groups;
	}

	
}
